﻿Public Class CancerResultHistory
    Private _citizen_id As String
    Private _afp As Nullable(Of Decimal)
    Private _psa As Nullable(Of Decimal)
    Private _cea As Nullable(Of Decimal)
    Private _ca_125 As Nullable(Of Decimal)
    Private _afp_abnormal_status As Nullable(Of Boolean)
    Private _psa_abnormal_status As Nullable(Of Boolean)
    Private _cea_abnormal_status As Nullable(Of Boolean)
    Private _ca_125_abnormal_status As Nullable(Of Boolean)

    Public Property Year As String

    Public Property CitizenID() As String
        Get
            Return _citizen_id
        End Get
        Set(ByVal value As String)
            _citizen_id = value
        End Set
    End Property

    Public Property Afp() As Nullable(Of Decimal)
        Get
            Return _afp
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            _afp = value
        End Set
    End Property

    Public Property Psa() As Nullable(Of Decimal)
        Get
            Return _psa
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            _psa = value
        End Set
    End Property

    Public Property Cea() As Nullable(Of Decimal)
        Get
            Return _cea
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            _cea = value
        End Set
    End Property

    Public Property Ca_125() As Nullable(Of Decimal)
        Get
            Return _ca_125
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            _ca_125 = value
        End Set
    End Property

    Public Property Afp_abnormal_status() As Nullable(Of Boolean)
        Get
            Return _afp_abnormal_status
        End Get
        Set(ByVal value As Nullable(Of Boolean))
            _afp_abnormal_status = value
        End Set
    End Property

    Public Property Psa_abnormal_status() As Nullable(Of Boolean)
        Get
            Return _psa_abnormal_status
        End Get
        Set(ByVal value As Nullable(Of Boolean))
            _psa_abnormal_status = value
        End Set
    End Property

    Public Property Cea_abnormal_status() As Nullable(Of Boolean)
        Get
            Return _cea_abnormal_status
        End Get
        Set(ByVal value As Nullable(Of Boolean))
            _cea_abnormal_status = value
        End Set
    End Property

    Public Property Ca_125_abnormal_status() As Nullable(Of Boolean)
        Get
            Return _ca_125_abnormal_status
        End Get
        Set(ByVal value As Nullable(Of Boolean))
            _ca_125_abnormal_status = value
        End Set
    End Property

    Public Property FullName As String
    Public Property Age As Integer
    Public Property OrganizationID As String
    Public Property OrganzationName As String
    Public Property CreatedUser As String
    Public Property CreateDate As Date
    Public Property UpdatedUser As String
    Public Property UpdateDate As Date

    'proc_SELECT_PoliceCancerResultHistory
    Public Shared Function GetPoliceCancerByYearAndOrganizationId(ByVal year As String, ByVal organizationId As String) As List(Of CancerResultHistory)
        Dim outResults As New List(Of CancerResultHistory)()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_PoliceCancerResultHistory"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inYear", year)
        objCmd.Parameters.Add(objParam)

        objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", organizationId)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "PoliceCancer")

        For Each row As DataRow In objDs.Tables("PoliceCancer").Rows
            Dim objDB As New CancerResultHistory()
            objDB.CitizenID = row.Item("citizen_id").ToString()
            objDB.FullName = row.Item("full_name").ToString()
            objDB.Age = row.Item("age")
            objDB.Afp = IIf(IsDBNull(row.Item("afp")), Nothing, row.Item("afp"))
            objDB.Psa = IIf(IsDBNull(row.Item("psa")), Nothing, row.Item("psa"))
            objDB.Cea = IIf(IsDBNull(row.Item("cea")), Nothing, row.Item("cea"))
            objDB.Ca_125 = IIf(IsDBNull(row.Item("ca_125")), Nothing, row.Item("ca_125"))
            outResults.Add(objDB)
        Next

        Return outResults
    End Function
    Public Shared Function CheckAFPAbnormal(ByVal afp As Nullable(Of Decimal)) As Nullable(Of Boolean)
        Dim outResult As Nullable(Of Boolean) = Nothing

        Select Case (afp)
            Case 0 To 0.85
                outResult = True
            Case Is < 0
                outResult = False
            Case Is > 0.85
                outResult = False
            Case Else
                outResult = Nothing
        End Select

        Return outResult
    End Function

    Public Shared Function CheckPSAAbnormal(ByVal psa As Nullable(Of Decimal)) As Nullable(Of Boolean)
        Dim outResult As Nullable(Of Boolean) = Nothing

        Select Case (psa)
            Case 0 To 4
                outResult = True
            Case Is < 0
                outResult = False
            Case Is > 4
                outResult = False
            Case Else
                outResult = Nothing
        End Select

        Return outResult
    End Function

    Public Shared Function CheckCEAAbnormal(ByVal cea As Nullable(Of Decimal)) As Nullable(Of Boolean)
        Dim outResult As Nullable(Of Boolean) = Nothing

        Select Case (cea)
            Case 0 To 5
                outResult = True
            Case Is < 0
                outResult = False
            Case Is > 5
                outResult = False
            Case Else
                outResult = Nothing
        End Select

        Return outResult
    End Function

    Public Shared Function CheckCA125Abnormal(ByVal ca125 As Nullable(Of Decimal)) As Nullable(Of Boolean)
        Dim outResult As Nullable(Of Boolean) = Nothing

        Select Case (ca125)
            Case 0 To 355
                outResult = True
            Case Is < 0
                outResult = False
            Case Is > 35
                outResult = False
            Case Else
                outResult = Nothing
        End Select

        Return outResult
    End Function

    Public Shared Function InsertPoliceCancerResultHistory(ByVal police As CancerResultHistory, ByRef errorMessage As String) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_INSERT_PoliceCancerResultHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inYear", Utilities.SendToDatabase(police.Year))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCitizenId", Utilities.SendToDatabase(police.CitizenID))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationId", Utilities.SendToDatabase(police.OrganizationID))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationName", Utilities.SendToDatabase(police.OrganzationName))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inAFP", Utilities.SendToDatabase(police.Afp))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inPSA", Utilities.SendToDatabase(police.Psa))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCEA", Utilities.SendToDatabase(police.Cea))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCA125", Utilities.SendToDatabase(police.Ca_125))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inAFPStatus", Utilities.SendToDatabase(CheckAFPAbnormal(police.Afp_abnormal_status)))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inPSAStatus", Utilities.SendToDatabase(CheckPSAAbnormal(police.Psa_abnormal_status)))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCEAStatus", Utilities.SendToDatabase(CheckCEAAbnormal(police.Cea_abnormal_status)))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCA125Status", Utilities.SendToDatabase(CheckCA125Abnormal(police.Ca_125_abnormal_status)))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreatedUser", police.CreatedUser)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreatedDate", police.CreateDate)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inUpdatedUser", police.UpdatedUser)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inUpdatedDate", police.UpdateDate)
            objCmd.Parameters.Add(objParam)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

            Return True
        Catch ex As Exception
            If ex.InnerException Is Nothing Then
                errorMessage = ex.Message
            Else
                errorMessage = ex.InnerException.Message
            End If

            Return False
        End Try

    End Function

End Class
